🔥 Top 70 C Programming Viva Questions with Detailed Answers – Part 2
(Loops, Functions & Recursion – Questions 26–42)
📌 Read Part 1 (Questions 1–25) here: [https://learninggrowthhub.blogspot.com/2026/02/top-70-c-programming-viva-questions.html]
📌 Continue to Part 3 (Questions 42–70): Coming Soon
🚀 Introduction
Loops, functions, recursion, and number-based logic problems are core areas tested in C programming viva and interviews.
Interviewers focus on:
-
Execution flow
-
Edge cases
-
Memory behavior
-
Time complexity
-
Logical correctness
-
Common mistakes
This section (Q26–Q50) covers strong interview-level programs with complete explanations and fully commented code.
🔹 LOOPS & NUMBER LOGIC SECTION
Q26) Print Multiplication Table of a Number
✅ Explanation
A loop repeatedly multiplies a number from 1 to 10.
Time Complexity: O(n)
💻 Program (Every Line Commented)
#include <stdio.h> // Include standard input-output library for printf and scanf
int main() // Main function where program execution begins
{ // Start of main function block
int num; // Declare integer variable 'num' to store user input
int i; // Declare integer variable 'i' to control loop iterations
printf("Enter a number: "); // Ask user to enter a number
scanf("%d", &num); // Read integer input and store it in variable 'num'
for(i = 1; i <= 10; i++) // Loop starts at 1, runs until 10, increments by 1 each time
{ // Start of for loop block
printf("%d x %d = %d\n", num, i, num * i); // Print multiplication result for each iteration
} // End of for loop block
return 0; // Return 0 to indicate successful execution
} // End of main function
🔎 Execution Insight
Loop executes exactly 10 times.
🎯 Viva Point
Used when iteration count is fixed.
⚠ Interview Insight
Wrong loop boundary causes incorrect output.
❌ Common Mistake
Starting from 0 instead of 1.
Q27) Check Prime Number
✅ Explanation
A prime number is divisible only by 1 and itself.
Optimized logic checks up to √n.
Time Complexity: O(√n)
#include <stdio.h> // Include input-output library
int main() // Program starts here
{ // Begin main block
int num; // Declare integer to store number
int i; // Loop counter variable
int flag = 1; // Assume number is prime (1 means true)
printf("Enter a number: "); // Prompt user
scanf("%d", &num); // Take input from user
if(num <= 1) // Prime numbers must be greater than 1
{
flag = 0; // Set flag to 0 (not prime)
}
for(i = 2; i * i <= num; i++) // Loop from 2 to square root of number
{
if(num % i == 0) // Check divisibility
{
flag = 0; // If divisible, not prime
break; // Exit loop early
}
}
if(flag == 1) // If flag still 1
printf("Prime Number"); // Print prime
else
printf("Not Prime Number"); // Otherwise print not prime
return 0; // End program successfully
} // End main
🔎 Execution Insight
Stops early if divisor found.
🎯 Viva Point
Checking till √n improves efficiency.
⚠ Interview Insight
Naive approach checking till n is inefficient.
❌ Common Mistake
Forgetting to handle numbers ≤ 1.
Q28) Reverse a Number
✅ Explanation
Extract digits using % operator and rebuild number.
#include <stdio.h> // Include standard library
int main() // Program entry
{ // Begin block
int num; // Variable to store original number
int reverse = 0; // Initialize reverse as 0
int remainder; // Variable to store extracted digit
printf("Enter a number: "); // Prompt user
scanf("%d", &num); // Read input
while(num != 0) // Loop until number becomes 0
{
remainder = num % 10; // Extract last digit
reverse = reverse * 10 + remainder; // Append digit to reversed number
num = num / 10; // Remove last digit
}
printf("Reversed Number = %d", reverse); // Display reversed result
return 0; // End program
} // End main
✅ Q29. Program to Check Prime Number💻 Program (Each Line Commented)
#include <stdio.h> // Include standard input-output header file
int main() // Main function starts
{ // Opening brace of main function
int num, i, flag = 1; // Declare num for input, i for loop, flag=1 means assume prime initially
printf("Enter a number: "); // Ask user to enter a number
scanf("%d", &num); // Store user input into variable num
if (num <= 1) // Check if number is less than or equal to 1
{ // Opening brace of if block
flag = 0; // Set flag to 0 because numbers <=1 are not prime
} // Closing brace of if block
else // Else block if number > 1
{ // Opening brace of else block
for (i = 2; i <= num / 2; i++) // Loop from 2 to num/2
{ // Opening brace of for loop
if (num % i == 0) // Check divisibility
{ // Opening brace of inner if
flag = 0; // Set flag to 0 (not prime)
break; // Exit loop early
} // Closing brace of inner if
} // Closing brace of for loop
} // Closing brace of else block
if (flag == 1) // Check final flag value
{ // Opening brace
printf("Prime Number"); // Print prime
} // Closing brace
else // Else condition
{ // Opening brace
printf("Not Prime"); // Print not prime
} // Closing brace
return 0; // End program successfully
} // Closing brace of main✅ Explanation
A prime number is divisible only by 1 and itself.
🎯 Viva Point
Checking up to
num/2reduces iterations.⚠ Interview Insight
Optimized solution checks up to
sqrt(num)for better performance.✅ Q30. Program to Find Factorial
💻 Program (Each Line Commented)
#include <stdio.h> // Include standard I/O library
int main() // Start main function
{ // Opening brace
int num, i; // Declare num for input and i for loop
long long fact = 1; // Use long long for large factorial values
printf("Enter a number: "); // Prompt user
scanf("%d", &num); // Read input value
if (num < 0) // Check for negative number
{
printf("Factorial not defined"); // Print error message
}
else
{
for (i = 1; i <= num; i++) // Loop from 1 to num
{
fact = fact * i; // Multiply fact by i
}
printf("Factorial = %lld", fact); // Print factorial
}
return 0; // End program
} // Closing brace✅ Explanation
Factorial of n = n × (n-1) × … × 1
🎯 Viva Point
Factorial of 0 is 1.
⚠ Interview Insight
Large factorials cause overflow even in
long long.
✅ Q31. Program to Print Fibonacci Series
💻 Program (Each Line Commented)
#include <stdio.h> // Include library
int main() // Start main
{ // Opening brace
int n, i; // n for terms, i for loop
int a = 0, b = 1, next; // First two Fibonacci numbers
printf("Enter number of terms: "); // Ask user
scanf("%d", &n); // Read input
for (i = 1; i <= n; i++) // Loop n times
{
printf("%d ", a); // Print current term
next = a + b; // Calculate next term
a = b; // Shift a to next value
b = next; // Shift b forward
}
return 0; // End program
} // Close main✅ Explanation
Each term is sum of previous two numbers.
🎯 Viva Point
Series starts from 0, 1.
⚠ Interview Insight
Interviewers may ask recursive version.
✅ Q32. Program to Reverse a Number
💻 Program (Each Line Commented)
#include <stdio.h> // Include library
int main() // Start main
{ // Opening brace
int num, rev = 0, rem; // Declare variables
printf("Enter number: "); // Prompt user
scanf("%d", &num); // Read input
while (num != 0) // Loop until num becomes 0
{
rem = num % 10; // Extract last digit
rev = rev * 10 + rem; // Build reversed number
num = num / 10; // Remove last digit
}
printf("Reverse = %d", rev); // Print result
return 0; // End program
} // Close main✅ Explanation
Digits are extracted and rebuilt in reverse order.
🎯 Viva Point
Use modulo
%to extract digits.⚠ Interview Insight
Be careful with negative numbers.
✅ Q33. Program to Check Armstrong Number
💻 Program (Each Line Commented)
#include <stdio.h> // Include library
int main() // Start main
{ // Opening brace
int num, temp, rem, sum = 0; // Declare variables
printf("Enter number: "); // Prompt user
scanf("%d", &num); // Read input
temp = num; // Store original number
while (temp != 0) // Loop until temp becomes 0
{
rem = temp % 10; // Extract digit
sum = sum + (rem * rem * rem); // Add cube of digit
temp = temp / 10; // Remove last digit
}
if (sum == num) // Compare sum with original
{
printf("Armstrong Number"); // Print if true
}
else
{
printf("Not Armstrong"); // Otherwise print false
}
return 0; // End program
} // Close main✅ Explanation
Armstrong number = sum of cubes of digits equals original number.
🎯 Viva Point
153 is classic Armstrong example.
⚠ Interview Insight
General case uses power equal to number of digits.
✅ Q34. Program to Check Prime Number💻 Program (Each Line Commented)
#include <stdio.h> // Include standard input-output header file
int main() // Main function starts
{ // Opening brace of main function
int num, i, flag = 1; // Declare num for input, i for loop, flag=1 means assume prime initially
printf("Enter a number: "); // Ask user to enter a number
scanf("%d", &num); // Store user input into variable num
if (num <= 1) // Check if number is less than or equal to 1
{ // Opening brace of if block
flag = 0; // Set flag to 0 because numbers <=1 are not prime
} // Closing brace of if block
else // Else block if number > 1
{ // Opening brace of else block
for (i = 2; i <= num / 2; i++) // Loop from 2 to num/2
{ // Opening brace of for loop
if (num % i == 0) // Check divisibility
{ // Opening brace of inner if
flag = 0; // Set flag to 0 (not prime)
break; // Exit loop early
} // Closing brace of inner if
} // Closing brace of for loop
} // Closing brace of else block
if (flag == 1) // Check final flag value
{ // Opening brace
printf("Prime Number"); // Print prime
} // Closing brace
else // Else condition
{ // Opening brace
printf("Not Prime"); // Print not prime
} // Closing brace
return 0; // End program successfully
} // Closing brace of main
✅ Explanation
A prime number is divisible only by 1 and itself.
🎯 Viva Point
Checking up to
num/2reduces iterations.⚠ Interview Insight
Optimized solution checks up to
sqrt(num)for better performance.
✅ Q35. Program to Find Factorial
💻 Program (Each Line Commented)
#include <stdio.h> // Include standard I/O library
int main() // Start main function
{ // Opening brace
int num, i; // Declare num for input and i for loop
long long fact = 1; // Use long long for large factorial values
printf("Enter a number: "); // Prompt user
scanf("%d", &num); // Read input value
if (num < 0) // Check for negative number
{
printf("Factorial not defined"); // Print error message
}
else
{
for (i = 1; i <= num; i++) // Loop from 1 to num
{
fact = fact * i; // Multiply fact by i
}
printf("Factorial = %lld", fact); // Print factorial
}
return 0; // End program
} // Closing brace
✅ Explanation
Factorial of n = n × (n-1) × … × 1
🎯 Viva Point
Factorial of 0 is 1.
⚠ Interview Insight
Large factorials cause overflow even in
long long.
✅ Q36. Program to Print Fibonacci Series
💻 Program (Each Line Commented)
#include <stdio.h> // Include library
int main() // Start main
{ // Opening brace
int n, i; // n for terms, i for loop
int a = 0, b = 1, next; // First two Fibonacci numbers
printf("Enter number of terms: "); // Ask user
scanf("%d", &n); // Read input
for (i = 1; i <= n; i++) // Loop n times
{
printf("%d ", a); // Print current term
next = a + b; // Calculate next term
a = b; // Shift a to next value
b = next; // Shift b forward
}
return 0; // End program
} // Close main
✅ Explanation
Each term is sum of previous two numbers.
🎯 Viva Point
Series starts from 0, 1.
⚠ Interview Insight
Interviewers may ask recursive version.
✅ Q37. Program to Reverse a Number
💻 Program (Each Line Commented)
#include <stdio.h> // Include library
int main() // Start main
{ // Opening brace
int num, rev = 0, rem; // Declare variables
printf("Enter number: "); // Prompt user
scanf("%d", &num); // Read input
while (num != 0) // Loop until num becomes 0
{
rem = num % 10; // Extract last digit
rev = rev * 10 + rem; // Build reversed number
num = num / 10; // Remove last digit
}
printf("Reverse = %d", rev); // Print result
return 0; // End program
} // Close main
✅ Explanation
Digits are extracted and rebuilt in reverse order.
🎯 Viva Point
Use modulo
%to extract digits.⚠ Interview Insight
Be careful with negative numbers.
✅ Q38. Program to Check Armstrong Number
💻 Program (Each Line Commented)
#include <stdio.h> // Include library
int main() // Start main
{ // Opening brace
int num, temp, rem, sum = 0; // Declare variables
printf("Enter number: "); // Prompt user
scanf("%d", &num); // Read input
temp = num; // Store original number
while (temp != 0) // Loop until temp becomes 0
{
rem = temp % 10; // Extract digit
sum = sum + (rem * rem * rem); // Add cube of digit
temp = temp / 10; // Remove last digit
}
if (sum == num) // Compare sum with original
{
printf("Armstrong Number"); // Print if true
}
else
{
printf("Not Armstrong"); // Otherwise print false
}
return 0; // End program
} // Close main
✅ Explanation
Armstrong number = sum of cubes of digits equals original number.
🎯 Viva Point
153 is classic Armstrong example.
⚠ Interview Insight
General case uses power equal to number of digits.
✅ Q39. Program to Check Perfect Number💻 Program (Each Line Commented)
#include <stdio.h> // Include standard input-output header file
int main() // Main function starts
{ // Opening brace of main function
int num, i, sum = 0; // Declare num for input, i for loop, sum to store sum of divisors
printf("Enter a number: "); // Ask user to enter a number
scanf("%d", &num); // Store user input in num
for (i = 1; i <= num / 2; i++) // Loop from 1 to num/2 to find proper divisors
{
if (num % i == 0) // Check if i is divisor of num
{
sum = sum + i; // Add divisor to sum
}
}
if (sum == num) // Compare sum of divisors with original number
{
printf("Perfect Number"); // Print if perfect
}
else
{
printf("Not Perfect"); // Print if not perfect
}
return 0; // End program successfully
} // Closing brace of main function
✅ Explanation
A perfect number is a number whose sum of proper divisors equals the number itself.
Example: 6 → 1 + 2 + 3 = 6🎯 Viva Point
We check only till
num/2because no divisor can be greater than half of the number.⚠ Interview Insight
For optimization, loop till
sqrt(num)and add divisor pairs.
✅ Q40. Program to Check Strong Number
💻 Program (Each Line Commented)
#include <stdio.h> // Include standard input-output header file
int factorial(int n) // Function to calculate factorial of a digit
{ // Opening brace of factorial function
int i, fact = 1; // Declare loop variable and initialize fact to 1
for (i = 1; i <= n; i++) // Loop from 1 to n
{
fact = fact * i; // Multiply fact by i
}
return fact; // Return calculated factorial
} // Closing brace of factorial function
int main() // Main function starts
{ // Opening brace of main function
int num, temp, rem, sum = 0; // Declare variables
printf("Enter a number: "); // Ask user for input
scanf("%d", &num); // Read input number
temp = num; // Store original number in temp
while (temp != 0) // Loop until temp becomes 0
{
rem = temp % 10; // Extract last digit
sum = sum + factorial(rem); // Add factorial of digit to sum
temp = temp / 10; // Remove last digit
}
if (sum == num) // Compare sum with original number
{
printf("Strong Number"); // Print if strong number
}
else
{
printf("Not Strong"); // Print if not strong
}
return 0; // End program successfully
} // Closing brace of main function
✅ Explanation
A Strong number is a number whose sum of factorial of digits equals the number.
Example: 145 → 1! + 4! + 5! = 145🎯 Viva Point
Uses a separate function for factorial → good modular programming practice.
⚠ Interview Insight
Interviewers like this question because it tests loops + functions + logic together.
✅ Q41. Program to Check Perfect Number💻 Program (Each Line Commented)
#include <stdio.h> // Include standard input-output header file
int main() // Main function starts
{ // Opening brace of main function
int num, i, sum = 0; // Declare num for input, i for loop, sum to store sum of divisors
printf("Enter a number: "); // Ask user to enter a number
scanf("%d", &num); // Store user input in num
for (i = 1; i <= num / 2; i++) // Loop from 1 to num/2 to find proper divisors
{
if (num % i == 0) // Check if i is divisor of num
{
sum = sum + i; // Add divisor to sum
}
}
if (sum == num) // Compare sum of divisors with original number
{
printf("Perfect Number"); // Print if perfect
}
else
{
printf("Not Perfect"); // Print if not perfect
}
return 0; // End program successfully
} // Closing brace of main function✅ Explanation
A perfect number is a number whose sum of proper divisors equals the number itself.
Example: 6 → 1 + 2 + 3 = 6🎯 Viva Point
We check only till
num/2because no divisor can be greater than half of the number.⚠ Interview Insight
For optimization, loop till
sqrt(num)and add divisor pairs.
✅ Q42. Program to Check Strong Number
💻 Program (Each Line Commented)
#include <stdio.h> // Include standard input-output header file
int factorial(int n) // Function to calculate factorial of a digit
{ // Opening brace of factorial function
int i, fact = 1; // Declare loop variable and initialize fact to 1
for (i = 1; i <= n; i++) // Loop from 1 to n
{
fact = fact * i; // Multiply fact by i
}
return fact; // Return calculated factorial
} // Closing brace of factorial function
int main() // Main function starts
{ // Opening brace of main function
int num, temp, rem, sum = 0; // Declare variables
printf("Enter a number: "); // Ask user for input
scanf("%d", &num); // Read input number
temp = num; // Store original number in temp
while (temp != 0) // Loop until temp becomes 0
{
rem = temp % 10; // Extract last digit
sum = sum + factorial(rem); // Add factorial of digit to sum
temp = temp / 10; // Remove last digit
}
if (sum == num) // Compare sum with original number
{
printf("Strong Number"); // Print if strong number
}
else
{
printf("Not Strong"); // Print if not strong
}
return 0; // End program successfully
} // Closing brace of main function✅ Explanation
A Strong number is a number whose sum of factorial of digits equals the number.
Example: 145 → 1! + 4! + 5! = 145🎯 Viva Point
Uses a separate function for factorial → good modular programming practice.
⚠ Interview Insight
Interviewers like this question because it tests loops + functions + logic together.
🎯 Final ConclusionMastering programs like Prime Number, Factorial, Fibonacci, Armstrong, Palindrome, GCD, LCM, Perfect Number, and Strong Number builds a strong logical foundation in C programming.
These programs are not just exam questions — they are:
🔹 Core logic builders
🔹 Frequently asked in viva
🔹 Very common in technical interviews
🔹 Essential for competitive programming basics
By understanding:
How loops work
How conditions control logic
How digits are extracted using
%How numbers are manipulated using division
How functions improve modularity
You are strengthening your problem-solving ability, not just memorizing syntax.
🚀 What You Have Learned in Part 2
✅ Number-based logic building
✅ Loop optimization techniques
✅ Efficient GCD using Euclidean algorithm
✅ Mathematical problem solving in C
✅ Function usage in real programs
✅ Interview-level thinking approach
💡 Important Advice for Students
Do not just copy the programs.
✔ Run them
✔ Change input values
✔ Try edge cases (0, negative numbers, large numbers)
✔ Modify logic yourself
That is how real programming confidence develops.
📌 Keep Learning. Keep Coding. Keep Growing.✨ Written by Krishna Popat
🌱 Founder, Learning Growth Hub
Comments
Post a Comment